import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import store from "../store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    // 元信息 => 为路由提供独立的描述配置，会存储在路由信息的 meta 对象中
    meta: {
      isPublic: true, // 该路由是公开路由无需登录
    },
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.meta.isPublic) {
    next();
    return;
  }
  if (store.state.token != null) {
    next();
    return;
  }
  alert("请先登录");
});

export default router;
